package main

import (
	"encoding/json"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"go.uber.org/zap"
)

func main() {
	// db, err := sql.Open("mysql", "root:123456@tcp(192.168.199.4:3306)/english?charset=utf8")
	// if err != nil {
	// 	fmt.Print(err)
	// }
	sugar := zap.NewExample().Sugar()
	defer sugar.Sync()
	sugar.Infow("failed to fetch URL",
		"url", "http://example.com",
		"attempt", 3,
		"backoff", time.Second,
	)
	sugar.Info("failed to fetch URL: %s", "http://example.com")

	// logger := zap.NewExample()
	// defer logger.Sync()
	// logger.Info("failed to fetch URL",
	// 	zap.String("url", "http://example.com"),
	// 	zap.Int("attempt", 3),
	// 	zap.Duration("backoff", time.Second),
	// )
	// logger.Info("log test")
	// db.SetMaxIdleConns(10)
	// fmt.Println(reflect.TypeOf(db), reflect.ValueOf(db).Kind())

	// backend := logging.NewLogBackend(os.Stdout, "", 0)
	// var format = logging.MustStringFormatter(
	// 	`%{color}%{time:15:04:05} %{shortfunc} %{level:.4s} ▶ %{message}`,
	// )
	// bf := logging.NewBackendFormatter(backend, format)
	// logging.SetBackend(bf)

	// var log = logging.MustGetLogger("example")
	// log.Info("test")
	// logging.Redact("test")

	rawJSON := []byte(`{
		"level": "debug",
		"encoding": "json",
		"outputPaths": ["stdout", "./logs"],
		"errorOutputPaths": ["stderr"],
		"initialFields": {"foo": "bar"},
		"encoderConfig": {
		  "messageKey": "message",
		  "timeEncoder":"",
		  "callerKey": "callers",
		  "callerEncoder": "short",
		  "stacktraceKey": "stack",
		  "lineEnding":"",

		  "timeKey": "time",
		  "levelKey": "level",
		  "levelEncoder": "lowercase"
		}
	  }`)

	/*
			func (e *TimeEncoder) UnmarshalText(text []byte) error {
			switch string(text) {
			case "iso8601", "ISO8601":
				*e = ISO8601TimeEncoder
			case "millis":
				*e = EpochMillisTimeEncoder
			case "nanos":
				*e = EpochNanosTimeEncoder
			default:
				*e = EpochTimeEncoder
			}
			return nil
		}
	*/

	var cfg zap.Config
	if err := json.Unmarshal(rawJSON, &cfg); err != nil {
		panic(err)
	}
	// cfg.EncoderConfig.EncodeTime = zapcore.EpochTimeEncoder
	// cfg.EncoderConfig.TimeKey = "time"

	logger, err := cfg.Build()
	if err != nil {
		panic(err)
	}
	defer logger.Sync()

	logger.Info("logger construction succeeded")

	logger = zap.NewExample()
	defer logger.Sync()
	logger.Info("failed to fetch URL",
		zap.String("url", "http://example.com"),
		zap.Int("attempt", 3),
		zap.Duration("backoff", time.Second),
	)
	time.Sleep(3 * time.Second)

	logger.Info("failed to fetch URL",
		zap.String("url", "http://example.com"),
		zap.Int("attempt", 3),
		zap.Duration("backoff", time.Second),
	)

	// atom := zap.NewAtomicLevel()

	// To keep the example deterministic, disable timestamps in the output.
	// encoderCfg := zap.NewProductionEncoderConfig()
	// encoderCfg.TimeKey = "ts"
	// logger2 := zap.New(zapcore.NewCore(
	// 	zapcore.NewJSONEncoder(encoderCfg),
	// 	zapcore.Lock(os.Stdout),
	// 	atom,
	// ))
	// defer logger2.Sync()

	// logger2.Info("info logging enabled")

	// atom.SetLevel(zap.ErrorLevel)
	// logger2.Info("info logging disabled")
}
